﻿using UnityEngine;
using System.Collections;
using QFramework;
using Cysharp.Threading.Tasks;
using DG.Tweening;
using Sirenix.OdinInspector;
using UnityEngine.SceneManagement;

public class Twir : PostEffectsBase,IController
{
	public Shader twirShader;
	private Material twirMaterial = null;
	public Material material 
	{  
		get {
			twirMaterial = CheckShaderAndCreateMaterial(twirShader, twirMaterial);
			return twirMaterial;
		}  
	}
	public float speed = 0.1f;
	public float noiseScale = 0;
	public float mask = 1f;
	public float myTime = 0;
	public bool gameOver = false;
	public bool gameWin = false;
	private void Start()
	{
		
	}

	public void OnGameOver(GameOver gameOverEvent)
	{
		gameOver = true;
		if(gameOverEvent.isSuccess)
		{
			gameWin = true;
			OnWin().Forget();
		}
	}


	void OnEnable() 
	{
		GetComponent<Camera>().depthTextureMode |= DepthTextureMode.DepthNormals;
	}

	[Button]
	private async UniTaskVoid OnWin() 
	{
		var audioSystem = this.GetSystem<AudioSystem>();
		audioSystem.PlaySoundEffect("Sound/effects/ufo_takeoff");
		
		
		await UniTask.Delay(3000);
		DOTween.To(()=> myTime,x => myTime = x,15,5);
		await DOTween.To(()=> noiseScale,x => noiseScale = x,15,3);
		await DOTween.To(()=> mask,x => mask = x,0,2);
		this.GetModel<StageModel>().needChange2Win = true;
		await this.GetSystem<UIManager>().OpenUIPanel("UIPanel/UIGameover/UIGameVictory");
		
		SceneManager.LoadScene("StartScene");
	}

	[ImageEffectOpaque]
	void OnRenderImage (RenderTexture src, RenderTexture dest) 
	{
		if (material != null && gameOver == true && gameWin == true) 
		{
			material.SetFloat("_NoiseScale", noiseScale);
			material.SetFloat("_Mask", mask);
			material.SetFloat("_MyTime", myTime);
			Graphics.Blit(src, dest, material,0);
		} else {
			Graphics.Blit(src, dest);
		}
	}

    public IArchitecture GetArchitecture() => GameArchitecture.Interface;
}
